package com.example.customcomwidget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Field;


/**
 * @author hdz
 * @date 2021/08/06 9:17 AM
 */
public class InputSettingsItem extends LinearLayout {

    private EditText etInput;

    public InputSettingsItem(Context context) {
        super(context);
        init(context, null);
    }

    public InputSettingsItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public InputSettingsItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.input_settings_item, this);
        TextView tvTitle = this.findViewById(R.id.title);
        this.etInput = this.findViewById(R.id.content);

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputSettingsItem);
            int titleColor = typedArray.getColor(R.styleable.InputSettingsItem_titleColor, Color.BLACK);
            tvTitle.setTextColor(titleColor);

            int titleSize = typedArray.getDimensionPixelSize(R.styleable.InputSettingsItem_titleSize, 12);
            tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);

            String titleText = typedArray.getString(R.styleable.InputSettingsItem_titleText);
            tvTitle.setText(titleText);

            int titleGravity = typedArray.getInt(R.styleable.InputSettingsItem_titleGravity, Gravity.CENTER_VERTICAL);
            tvTitle.setGravity(titleGravity);


            int inputColor = typedArray.getColor(R.styleable.InputSettingsItem_inputColor, Color.BLACK);
            this.etInput.setTextColor(inputColor);

            float inputSize = typedArray.getDimensionPixelSize(R.styleable.InputSettingsItem_inputSize, 12);
            this.etInput.setTextSize(TypedValue.COMPLEX_UNIT_PX, inputSize);

            String inputText = typedArray.getString(R.styleable.InputSettingsItem_inputText);
            this.etInput.setText(inputText);

            int inputGravity = typedArray.getInt(R.styleable.InputSettingsItem_inputGravity, Gravity.CENTER_VERTICAL | Gravity.END);
            this.etInput.setGravity(inputGravity);

            int bgId = typedArray.getResourceId(R.styleable.InputSettingsItem_inputBackground, 0);
            if (bgId != 0) {
                Drawable drawable = getResources().getDrawable(bgId);
                if (drawable != null) {
                    this.etInput.setBackground(drawable);
                }
            }
            int cursorId = typedArray.getResourceId(R.styleable.InputSettingsItem_inputCursorDrawable, 0);
            if (cursorId != 0) {
                Drawable drawable = getResources().getDrawable(cursorId);
                if (drawable != null) {
                    try {
                        Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
                        f.setAccessible(true);
                        f.set(this.etInput, drawable);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            int type = typedArray.getInt(R.styleable.InputSettingsItem_android_inputType, -1);
            if (type != -1) {
                this.etInput.setInputType(type);
            }

            typedArray.recycle();
        }
    }

    public void setText(String str) {
        this.etInput.setText(str);
    }

    public String getText() {
        return this.etInput.getText().toString();
    }

    public void addTextChangedListener(TextWatcher watcher) {
        this.etInput.addTextChangedListener(watcher);
    }
}
